package thesis;
import java.util.Hashtable;
import java.util.List;

import com.mongodb.DBObject;


public class DistanceCalculator {
	private static final double UCDISTWEIGHT = 0.334;
	private static final double TBCDISTWEIGHT = 0.333;
	private static final double TDISTWEIGHT = 0.333;

	public double computeDistance(DBObject tweet1, DBObject tweet2) {
		double UCDistance = computeUCDistance(tweet1, tweet2);
		
		double TBCDistance = computeTCDistance(tweet1, tweet2);
		
		double TDistance = computeTDistance(tweet1, tweet2);
	
		return (UCDISTWEIGHT * UCDistance) + (TBCDISTWEIGHT * TBCDistance) + (TDISTWEIGHT * TDistance);
	}

	private double computeTDistance(DBObject tweet1, DBObject tweet2) {
		InternTweetInfoExtractor infoExtr1 = new InternTweetInfoExtractor(tweet1);
		InternTweetInfoExtractor infoExtr2 = new InternTweetInfoExtractor(tweet2);
		
		String timeCat1 = infoExtr1.getTimeCategory();
		String timeCat2 = infoExtr2.getTimeCategory();
		
		int cat1 = Integer.parseInt(timeCat1.substring(1));
		int cat2 = Integer.parseInt(timeCat2.substring(1));
		
		int absDist = Math.abs(cat1 - cat2);
		int maxDist = TimeManager.getNumberOfTimeCategories();
		
		double lDistance = (double)absDist / maxDist;
		
		return lDistance;
	}

	private double computeUCDistance(DBObject tweet1, DBObject tweet2){
		InternTweetInfoExtractor infoExtr1 = new InternTweetInfoExtractor(tweet1);
		List<Relevance> relevances1 = infoExtr1.getRelevanceList();
		
		Hashtable<String, Double> relevanceTable1 = new Hashtable<String, Double>();
		for (Relevance r : relevances1){
			relevanceTable1.put(r.getConceptId(), r.getRelevance());
		}
		
		InternTweetInfoExtractor infoExtr2 = new InternTweetInfoExtractor(tweet2);
		List<Relevance> relevances2 = infoExtr2.getRelevanceList();
		
		Hashtable<String, Double> relevanceTable2 = new Hashtable<String, Double>();
		for (Relevance r : relevances2){
			relevanceTable2.put(r.getConceptId(), r.getRelevance());
		}
		
		List<Concept> concepts = FSModule.getConcepts();
		int numberOfConcepts = concepts.size();
		
		double ucDistance = 0;
		
		for (Concept concept: concepts){
			String conceptId = concept.getId();
			
			double conceptVal1 = 0;
			if (relevanceTable1.get(conceptId) != null){
				conceptVal1 = relevanceTable1.get(conceptId);
			}
			
			double conceptVal2 = 0;
			if (relevanceTable2.get(conceptId) != null){
				conceptVal2 = relevanceTable2.get(conceptId);
			}
			
			double conceptDistance = conceptVal1 - conceptVal2;
			ucDistance += Math.pow(conceptDistance, 2);
		}
		
		return Math.sqrt(ucDistance/numberOfConcepts);
	}
	
	private double computeTCDistance(DBObject tweet1, DBObject tweet2){
		InternTweetInfoExtractor infoExtr1 = new InternTweetInfoExtractor(tweet1);
		long tweetId1 = infoExtr1.getId();
		
		InternTweetInfoExtractor infoExtr2 = new InternTweetInfoExtractor(tweet2);
		long tweetId2 = infoExtr2.getId();
		
		String cluster1 = FSModule.readClusterWithTweetId(tweetId1);
		
		String cluster2 = FSModule.readClusterWithTweetId(tweetId2);
		
		if (cluster1.compareTo(cluster2) == 0){
			return 0;
		}
		else{
			return 1;
		}
		
	}
}
